
//
// 120120 - AH - Created.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.ptable;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.soekrat.AttributeObject;
import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.ontology.ptable.ProductTable;
import org.soekrat.engine.ontology.ptable.ProductTableFactor;

/**
 * <p>
 *   View to edit 
 *   {@link ProductTable}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ProductTableView extends JPanel
{
  private ProductTableTableModel m_model=new ProductTableTableModel();
  
  private final JTable m_table=new JTable(m_model);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced.
   */
  public ProductTableView(Soekrat2Engine engine)
  {
    setLayout(new BorderLayout());
    
    setProductTable(engine.getWheel().getProductTable());
   
    add(new JScrollPane(m_table), BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add(new JButton(m_addRowAction));
    buttons.add(new JButton(m_addColAction));
    buttons.add(new JButton(m_removeFactorAction));
    add(buttons, BorderLayout.SOUTH);
  }
  
  private ProductTableFactor m_factorEdited=null;
  
  private boolean m_rowEdited=false;
  
  void setEdited(Object v, int row, int col)
  {
    m_removeFactorAction.setEnabled(false);
    m_factorEdited=null;
    m_rowEdited=false;
    if (v instanceof ProductTableFactor)
    {
      m_removeFactorAction.setEnabled(true);
      m_factorEdited=(ProductTableFactor)v;
      m_rowEdited=(col==0);
    }
  }
  
  private final Action m_removeFactorAction=new AbstractAction("Remove Factor")
  {  
    {
      setEnabled(false);
    }
    
    public void actionPerformed(ActionEvent e) 
    {
      if (m_rowEdited) m_model.removeRow(m_factorEdited);
      else             m_model.removeCol(m_factorEdited);
    }
  };
  
  private Action m_addRowAction=new AbstractAction("New Row")
  {
    public void actionPerformed(ActionEvent e)
    {
      m_model.addRow();
    }
  };
  
  private Action m_addColAction=new AbstractAction("New Column")
  {
    public void actionPerformed(ActionEvent e)
    {
      m_model.addColumn();
    }
  };
  
  public void setProductTable(ProductTable pt)
  {
    m_model=(pt==null) ? new ProductTableTableModel() : 
                         new ProductTableTableModel(pt);
    
    m_table.setDefaultRenderer(AttributeObject.class, 
      new ProductTableCellRenderer());
    m_table.setDefaultEditor(AttributeObject.class, 
      new ProductTableTableCellEditor(this));
    
    m_table.setModel(m_model);
  }
  
}
